package com.yanqu.road.dao.impl.wingroom.chakra;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.chakra.WingRoomChakraPatronsAddItem;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomFurnitureRewardRecord;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WingRoomChakraDetailDaoImpl extends TempDataObjectDao<UserWingRoomChakraDetail> {

    private static String insertSql = "INSERT INTO `t_u_wingroom_chakra_detail`(`userId`, `occupation`, `type`, `position`, `wxType`, `level`, `quality`, `patronsList`, `createTime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(UserWingRoomChakraDetail data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOccupation()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWxType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQuality()));
        String patronsJson = JSONObject.toJSONString(data.getPatronsList());
        para.put(i++, new DbParameter(Types.VARCHAR, patronsJson));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    private String updateSql = "UPDATE `t_u_wingroom_chakra_detail` SET `wxType` = ?, `level` = ?, `quality` = ?, `patronsList` = ?, `updateTime` = ? WHERE `userId` = ? AND `occupation` = ? AND `type` = ? AND `position` = ?";
    @Override
    protected boolean updateData(UserWingRoomChakraDetail data) {
        updateSql = "REPLACE INTO `t_u_wingroom_chakra_detail`(`userId`, `occupation`, `type`, `position`, `wxType`, `level`, `quality`, `patronsList`, `createTime`,`updateTime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOccupation()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPosition()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWxType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQuality()));
        String patronsJson = JSONObject.toJSONString(data.getPatronsList());
        para.put(i++, new DbParameter(Types.VARCHAR, patronsJson));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomChakraDetail parseData(ResultSet rs) throws SQLException {
        UserWingRoomChakraDetail data = new UserWingRoomChakraDetail();
        data.setUserId(rs.getLong("userId"));
        data.setOccupation(rs.getInt("occupation"));
        data.setType(rs.getInt("type"));
        data.setPosition(rs.getInt("position"));
        data.setWxType(rs.getInt("wxType"));
        data.setLevel(rs.getInt("level"));
        data.setQuality(rs.getInt("quality"));
        JSONArray jsonArray = JSONObject.parseArray(rs.getString("patronsList"));
        List<WingRoomChakraPatronsAddItem> patronsList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            WingRoomChakraPatronsAddItem item = new WingRoomChakraPatronsAddItem();
            item.setPatronsId(jsonObject.getIntValue("patronsId"));
            item.setType(jsonObject.getIntValue("type"));
            item.setAddValue(jsonObject.getLongValue("addValue"));
            if (jsonObject.containsKey("addAbility")) {
                item.setAddAbility(jsonObject.getLongValue("addAbility"));
            }
            patronsList.add(item);
        }
        data.setPatronsList(patronsList);
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }

    /**
     * 获取命盘详情数据
     * @param userId
     * @return
     */
    public Map<Integer, Map<Integer, Map<Integer, UserWingRoomChakraDetail>>> getUserWingRoomChakraDetailMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, Map<Integer, UserWingRoomChakraDetail>>> map = new ConcurrentHashMap<>();
        String sql = "select * from t_u_wingroom_chakra_detail where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWingRoomChakraDetail data = parse(rs);
                Map<Integer, Map<Integer, UserWingRoomChakraDetail>> typeMap = map.computeIfAbsent(data.getOccupation(), k -> new HashMap<>());
                Map<Integer, UserWingRoomChakraDetail> positionMap = typeMap.computeIfAbsent(data.getType(), k -> new HashMap<>());
                positionMap.put(data.getPosition(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
